package bb.lanxing.device.sgdevcie.utils;

import com.garmin.fit.Mesg;
import com.garmin.fit.SessionMesg;

import java.io.File;

import bb.lanxing.devices.sync.DefaultFitProcessor;
import bb.lanxing.lib.devices.sync.FitManager;
import bb.lanxing.lib.devices.sync.FitTrans;
import kotlin.Lazy;
import kotlin.LazyKt;
import kotlin.Result;
import kotlin.ResultKt;
import kotlin.Unit;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.SafeContinuation;
import kotlin.coroutines.intrinsics.IntrinsicsKt;
import kotlin.coroutines.jvm.internal.DebugProbesKt;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.internal.DefaultConstructorMarker;
import kotlin.jvm.internal.Intrinsics;

public final class FitParser {
    private Continuation<? super Boolean> continuation;
    private final int deviceType;
    private final Lazy fitManager$delegate;
    private final FitParser$fitManagerCallback$1 fitManagerCallback;
    private final Lazy fitProcessor$delegate;

    public interface FitParserListener {
        void onDone(boolean z, Throwable th);

        void onStart();
    }

    public FitParser() {
        this(0, 1, null);
    }

    public FitParser(int i) {
        this.deviceType = i;
        this.fitManager$delegate = LazyKt.lazy(new FitParser$fitManager$2(this));
        this.fitProcessor$delegate = LazyKt.lazy(new FitParser$fitProcessor$2(this));
        this.fitManagerCallback = new FitParser$fitManagerCallback$1(this);
    }

    public /* synthetic */ FitParser(int i, int i2, DefaultConstructorMarker defaultConstructorMarker) {
        this((i2 & 1) != 0 ? 18 : i);
    }

    public final int getDeviceType() {
        return this.deviceType;
    }

    public final Continuation<Boolean> getContinuation() {
        return (Continuation<Boolean>) this.continuation;
    }

    public final void setContinuation(Continuation<? super Boolean> continuation) {
        this.continuation = continuation;
    }

    public final FitManager getFitManager() {
        return (FitManager) this.fitManager$delegate.getValue();
    }

    public final DefaultFitProcessor getFitProcessor() {
        return (DefaultFitProcessor) this.fitProcessor$delegate.getValue();
    }

    public final Object decode(File file, Continuation<? super Boolean> continuation) {
        SafeContinuation safeContinuation = new SafeContinuation(IntrinsicsKt.intercepted(continuation));
        this.continuation = safeContinuation;
        FitParser$fitManagerCallback$1 fitParser$fitManagerCallback$1 = this.fitManagerCallback;
        FitTrans fitTrans = FitTrans.get();
        fitTrans.setId(System.currentTimeMillis());
        fitTrans.setPath(file.getPath());
        fitParser$fitManagerCallback$1.onFitReceived(fitTrans);
        Object orThrow = safeContinuation.getOrThrow();
        if (orThrow == IntrinsicsKt.getCOROUTINE_SUSPENDED()) {
            DebugProbesKt.probeCoroutineSuspended(continuation);
        }
        return orThrow;
    }

    public final void setAddress(String address) {
        Intrinsics.checkNotNullParameter(address, "address");
        getFitProcessor().setAddress(address);
    }

    public final void release() {
        getFitManager().release();
    }

    public static final class FitParser$fitManagerCallback$1 implements FitManager.FitManagerCallback {
        final /* synthetic */ FitParser this$0;

        public FitParser$fitManagerCallback$1(FitParser fitParser) {
            this.this$0 = fitParser;
        }

        @Override
        public void onFitReceived(FitTrans fitTrans) {
            this.this$0.getFitProcessor().onStart(fitTrans);
            this.this$0.getFitManager().decode(fitTrans);
        }

        @Override
        public void onSport(FitTrans fitTrans, SessionMesg sessionMesg) {
            this.this$0.getFitProcessor().onSport(sessionMesg);
        }

        @Override
        public void onMesg(FitTrans fitTrans, Mesg mesg) {
            if (mesg == null) {
                this.this$0.getFitProcessor().onComplete(fitTrans);
                Continuation<Boolean> continuation = this.this$0.getContinuation();
                if (continuation == null) {
                    return;
                }
                Result.Companion companion = Result.Companion;
                continuation.resumeWith(true);
                return;
            }
            this.this$0.getFitProcessor().onMesg(mesg);
        }

        @Override
        public void onError(FitTrans fitTrans, Throwable th) {
            Unit unit;
            if (th != null) {
                Continuation<Boolean> continuation = this.this$0.getContinuation();
                if (continuation != null) {
                    Result.Companion companion = Result.Companion;
                    //TODO:hu
                    continuation.resumeWith(ResultKt.createFailure(th));
                    unit = Unit.INSTANCE;
                } else {
                    unit = null;
                }
                if (unit != null) {
                    return;
                }
            }
            Continuation<Boolean> continuation2 = this.this$0.getContinuation();
            if (continuation2 != null) {
                Result.Companion companion2 = Result.Companion;
                continuation2.resumeWith(false);
                Unit unit2 = Unit.INSTANCE;
            }
        }
    }

    public static final class FitParser$fitManager$2 implements Function0<FitManager> {
        final /* synthetic */ FitParser this$0;

        public FitParser$fitManager$2(FitParser fitParser) {
            super();
            this.this$0 = fitParser;
        }

        @Override
        public final FitManager invoke() {
            FitParser$fitManagerCallback$1 fitParser$fitManagerCallback$1;
            fitParser$fitManagerCallback$1 = this.this$0.fitManagerCallback;
            return new FitManager(null, fitParser$fitManagerCallback$1);
        }
    }

    public static final class FitParser$fitProcessor$2 implements Function0<DefaultFitProcessor> {
        final /* synthetic */ FitParser this$0;

        public FitParser$fitProcessor$2(FitParser fitParser) {
            super();
            this.this$0 = fitParser;
        }

        @Override
        public final DefaultFitProcessor invoke() {
            return new DefaultFitProcessor(this.this$0.getDeviceType());
        }
    }


}
